package com.uairobot.bill.tomcat;

import java.io.File;
import java.net.URL;

import com.litong.utils.file.ConfigUtil;

public class TomcatUtil {
  private static URL webappsPath;
  private static File webappFile;
  private static ClassLoader classLoader;

  public static URL getWebappsPath() {
    if (webappsPath == null) {
      webappsPath = getClassLoader().getResource("webapps");
    }
    return webappsPath;
  }

  public static File getWebappsFile() {
    if (webappFile == null) {
      URL webappPath = getWebappsPath();
      if (webappPath == null) {
        String file = getClassLoader().getResource("").getFile();
        System.out.println("webappPath is null,auto create webapps in " + file);
        webappFile = new File(file + "webapps");
        if (!webappFile.exists()) {
          webappFile.mkdirs();
        }
      } else {
        webappFile = new File(webappPath.getFile());
      }
    }
    return webappFile;
  }

  public static ClassLoader getClassLoader() {
    if (classLoader == null) {
      classLoader = TomcatUtil.class.getClassLoader();
    }
    return classLoader;
  }

  public static int getPort() {
    return ConfigUtil.getIntValue(Constants.tomcatPort);
  }
}
